home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / du_lib / ed_text.c < prev    next >
C/C++ Source or Header  |  1995-07-10  |  6KB  |  275 lines

  1. /*
  2.    DU_LIB v2
  3.    Gem Window Management & Dialog Library For Lattice C
  4.    ½1994,95, by Craig Graham.
  5.    Based on the DU_LIBv1 Library for HiSoft Basic.
  6.  
  7. */
  8.  
  9. #include "DULIB.H"
  10.  
  11. #define DIALOG_LAYER_KEY_HANDLER    -2
  12.  
  13. /*
  14.     EDITABLE TEXT OBJECTS
  15. */
  16.  
  17. /*
  18.     Make a text object edittable
  19. */
  20. void Set_object_editable(short dialog,short object)
  21. {
  22.     OBJECT *t;
  23.     TEDINFO *te;
  24.     
  25.     Set_object_Kcallback(dialog,object,&ted_callback);
  26.     Set_object_callback(dialog,object,&ted_mouseclick);
  27.     Set_object_redraw(dialog,object,&ted_display_cursor);
  28.     
  29.     rsrc_gaddr(0,dialog,&t);
  30.     t+=object;
  31.     te=(TEDINFO*)t->ob_spec;
  32.     
  33.     te->te_tmplen=0;    // Use te_tmplen field to store cursor position instead
  34.     dialog_details[dialog].current_focus=object;    // Ensure we have a valid initial editable object
  35.     Set_dialog_Kfocus_policy(dialog, FOCUS_FIELD);
  36. }
  37.  
  38. /*
  39.     Set the focus policy for a dialog
  40.     Options are:
  41.     FOCUS_MOUSE : key to object under mouse
  42.     FOCUS_FIELD : key to object with keyboard focus
  43. */
  44. void Set_dialog_Kfocus_policy(short dialog, short focus_policy)
  45. {
  46.     dialog_details[dialog].focus_mode=focus_policy;
  47. }
  48.  
  49. /* Set the keyboard focus for a dialog, change highlight if dialog is open */
  50. void Set_dialog_Kfocus(short dialog, short object)
  51. {
  52.     short n,wind,x,y,w,h,top_w;
  53.     OBJECT *t;
  54.     TEDINFO *te;
  55.     
  56.     rsrc_gaddr(0,dialog,&t);
  57.     
  58.     wind=0;
  59.     for(n=0; (n<max_windows)&&(wind==0); n++)
  60.         if ((windows[n].window_type!=wt_null)&&(windows[n].the_dialog==dialog))
  61.             wind=n;
  62.     
  63.     if (wind)
  64.     {
  65.         wind_get(wind,WF_WORKXYWH,&x,&y,&w,&h);
  66.         t->ob_x=x;
  67.         t->ob_y=y;
  68.     }
  69.     wind_get(cr_wind_handle,WF_TOP,&top_w,0,0,0);
  70.  
  71.     graf_mouse(M_OFF,NULL);
  72.     wind_update(BEG_UPDATE);
  73.  
  74.     if (dialog_details[dialog].current_focus)
  75.     {
  76.         te=(TEDINFO*)(t+dialog_details[dialog].current_focus)->ob_spec;
  77.  
  78.         te->te_thickness=1;
  79.     
  80.         if ((wind)&&(wind==top_w))
  81.             objc_draw(t,dialog_details[dialog].current_focus,1,scrn_x,scrn_y,scrn_w,scrn_h);
  82.     }
  83.  
  84.     dialog_details[dialog].current_focus=object;
  85.     te=(TEDINFO*)(t+dialog_details[dialog].current_focus)->ob_spec;
  86.  
  87.     te->te_thickness=2;
  88.  
  89.     if (wind)
  90.     {
  91.         if (wind==top_w)
  92.         {
  93.             objc_draw(t,dialog_details[dialog].current_focus,1,scrn_x,scrn_y,scrn_w,scrn_h);
  94.         }else{
  95.             dialog_display(dialog);
  96.         }
  97.     }
  98.     
  99.     wind_update(END_UPDATE);
  100.     graf_mouse(M_ON,NULL);
  101. }
  102.  
  103. short ted_callback(void)
  104. {
  105.     OBJECT *t;
  106.     TEDINFO *te;
  107.     Elist *e;
  108.     short curpos,o;
  109.     short rtn;
  110.     short f;
  111.     char *text;
  112.     short c;
  113.  
  114.     rtn=0;
  115.     rsrc_gaddr(0,this_dialog,&t);
  116.     
  117.     te=(TEDINFO*)(t+this_ob)->ob_spec;
  118.     
  119.     text=te->te_ptext;
  120.     curpos=te->te_tmplen;
  121.     
  122.     switch(kc_key)
  123.     {
  124.         case 0x4d00:            // Cursor right
  125.             if (curpos<te->te_txtlen-1)
  126.                 te->te_tmplen++;
  127.             rtn=1;
  128.             break;
  129.         case 0x4b00:            // Cursor left
  130.             if (curpos>0) te->te_tmplen--;
  131.             rtn=1;
  132.             break;
  133.         case 0x4d36:            // Shift+Cursor right
  134.             te->te_tmplen=te->te_txtlen-1;
  135.             rtn=1;
  136.             break;
  137.         case 0x4b34:            // Shift+Cursor left
  138.             te->te_tmplen=0;
  139.             rtn=1;
  140.             break;
  141.         case 0x5000:            // Next field: several characters do this, cursor down
  142.         case 0x0f09:            // Tab
  143.             e=event_value[this_dialog];
  144.             for(o=e->object; (e!=NULL)&&(o!=this_ob); )
  145.             {
  146.                 o=e->object;
  147.                 if (o!=this_ob) { e=e->next; }
  148.             }
  149.             if (o==this_ob)
  150.             {
  151.                 e=e->next;
  152.                 if (e!=NULL)
  153.                 {
  154.                     for(o=e->object; (e!=NULL)&&(e->Kcallback==NULL); )
  155.                     {
  156.                         o=e->object;
  157.                         if (e->Kcallback==NULL) { e=e->next; }
  158.                     }
  159.                 }
  160.                 if (e==NULL)
  161.                 {
  162.                     e=event_value[this_dialog];
  163.                     for(o=e->object; (e!=NULL)&&(e->Kcallback==NULL); )
  164.                     {
  165.                         o=e->object;
  166.                         if (e->Kcallback==NULL) { e=e->next; }
  167.                     }
  168.                 }
  169.                 Set_dialog_Kfocus(this_dialog, o);
  170.             }
  171.             rtn=1;
  172.             break;
  173.         case 0x4800:            // Cursor up - ignore
  174.             rtn=1;
  175.             break;
  176.         case 0x011b:            // Escape - clear field
  177.             for(f=0; f<te->te_txtlen-1; f++) text[f]=' ';
  178.             te->te_tmplen=0;
  179.             rtn=1;
  180.             break;
  181.         case 0x0e08:            // Backspace
  182.             if (curpos)
  183.             {
  184.                 for(f=curpos-1; f<te->te_txtlen-2; f++) text[f]=text[f+1];
  185.                 text[te->te_txtlen-1]=' ';
  186.                 te->te_tmplen--;
  187.             }
  188.             rtn=1;
  189.             break;
  190.         case 0x537f:            // Delete
  191.             for(f=curpos; f<te->te_txtlen-2; f++) text[f]=text[f+1];
  192.             text[te->te_txtlen-1]=' ';
  193.             rtn=1;
  194.             break;
  195.     }
  196.     
  197.     if ((rtn==0)&&(kc_shstate&(K_CTRL|K_ALT))) rtn=2;    // Prevent alt/ctrl from being processed 
  198.                                                         // - allow keyboard shortcuts to get through
  199.     
  200.     if (!rtn)
  201.     {
  202.         c=(short)(kc_key&255);        // Convert scan code into ASCII character.
  203.         if (c>31)
  204.         {
  205.             text[curpos]=(char)c;
  206.  
  207.             if (curpos<te->te_txtlen-1)
  208.                 te->te_tmplen++;
  209.             
  210.             rtn=1;
  211.         }
  212.     }
  213.  
  214.     objc_draw(t,this_ob,0,cr_clip.g_x,cr_clip.g_y,cr_clip.g_w,cr_clip.g_h);
  215.  
  216.     ted_display_cursor();
  217.  
  218.     if (rtn==1)
  219.         return TRUE;
  220.     else
  221.         return FALSE;
  222. }
  223.  
  224. short ted_display_cursor(void)
  225. {
  226.     OBJECT *t;
  227.     TEDINFO *te;
  228.     short curpos,pt[8];
  229.     char *text,c,cc[2];
  230.     short th,lw,cw;
  231.     
  232.     if (dialog_details[this_dialog].current_focus!=this_ob) return TRUE;
  233.     
  234.     rsrc_gaddr(0,this_dialog,&t);
  235.     t+=this_ob;
  236.     
  237.     te=(TEDINFO*)t->ob_spec;
  238.     curpos=te->te_tmplen;
  239.     text=te->te_ptext;
  240.     c=text[curpos];
  241.     text[curpos]='\0';
  242.     vqt_extent(x_handle, text, pt);
  243.     text[curpos]=c;
  244.     th=pt[1]-pt[7];
  245.     lw=pt[2]-pt[0];
  246.     cc[0]=c; cc[1]='\0';
  247.     vqt_extent(x_handle, cc, pt);
  248.     cw=pt[2]-pt[0];
  249.     
  250.     vswr_mode(x_handle,MD_XOR);
  251.  
  252.     pt[0]=cr_clip.g_x+lw+1; pt[1]=cr_clip.g_y+2;
  253.     pt[2]=cr_clip.g_x+lw+cw; pt[3]=cr_clip.g_y+cr_clip.g_h-4;
  254.  
  255.     vsf_color(x_handle, BLACK);
  256.     vsf_style(x_handle, FIS_SOLID);
  257.     
  258.     v_bar(x_handle, pt);
  259.     
  260.     vswr_mode(x_handle,MD_TRANS);
  261.     
  262.     return FALSE;
  263. }
  264.  
  265. short ted_mouseclick(void)
  266. {
  267.     short mb,dummy;
  268.     
  269.     Set_dialog_Kfocus(this_dialog, this_ob);
  270.     
  271.     do { graf_mkstate(&dummy,&dummy,&mb,&dummy); } while (mb);
  272.     
  273.     return TRUE;
  274. }
  275.